﻿namespace Minesweeper
{
    using System;
    using System.IO;

    class Menu
    {
        private static LevelHandler levelHandler = new LevelHandler();
        public static Level defaultLevel = Level.Beginner;

        public static void SetConsoleForMenu()
        {
            Console.CursorVisible = false;
            ConsoleExtender.ConsoleHelper.SetConsoleFont(MinesweeperGame.FontIndex);
            try
            {
                Console.WindowHeight = 26;
                Console.WindowWidth = 80;
            }
            catch (ArgumentOutOfRangeException)
            {
                MinesweeperGame.FontIndex--;
            }
            Console.Clear();
            Console.SetBufferSize(80, 26);
            Console.Title = "JustMines - Team Dazzler";
        }

        public static MineField CreatePlayField(Level level)
        {
            LevelData levelData = levelHandler.GenerateLevelData(level);
            return new MineField(levelData.FieldHeight, levelData.FieldWidth, levelData.MinesCount);
        }

        public static void SelectMenu()
        {
            SetConsoleForMenu();
            int userChoice = 0;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(0, 0);

            string[] justMinesTitle = 
                {
                    @"                 _              _     __  __  _                   ",
                    @"                | |            | |   |  \/  |(_)                  ",
                    @"                | | _   _  ___ | |_  | \  / | _  _ __    ___  ___ ",
                    @"            _   | || | | |/ __|| __| | |\/| || || '_ \  / _ \/ __|",
                    @"           | |__| || |_| |\__ \| |_  | |  | || || | | ||  __/\__ \",
                    @"            \____/  \__,_||___/ \__| |_|  |_||_||_| |_| \___||___/"
                };

            foreach (string line in justMinesTitle)
            {
                Console.WriteLine(line);
            }

            Console.ForegroundColor = ConsoleColor.Cyan;
            string spaces = new string(' ', 30);
            Console.WriteLine();
            Console.WriteLine(@"                           New game");
            levelHandler.CreateLevelMenu();
            Console.WriteLine("{0} Instructions", spaces);
            Console.WriteLine("{0} Highscores", spaces);
            Console.WriteLine("{0} Exit", spaces);
            Console.WriteLine("\n\n\n");
            string separator = new string('-', 30);
            Console.ForegroundColor = ConsoleColor.DarkMagenta;

            string[] dynamite = 
                {
                    @"            ______________________________       . \  | / . ",
                    @"           /                            / \        \ \ / /",
                    @"           |       Team Dazzler         |{========= >- -<",
                    @"           \____________________________\_/        / / \ \",
                    @"                                                  . /  | \ .",
                };

            foreach (string line in dynamite)
            {
                Console.WriteLine(line);
            }

            int arrowX = 28;
            int arrowY = 9 + userChoice;

            while (true)
            {
                Console.CursorVisible = false;
                Console.SetCursorPosition(arrowX, arrowY);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("->");
                ConsoleKeyInfo key = Console.ReadKey(true);

                if (key.Key == ConsoleKey.DownArrow)
                {
                    if (userChoice < 5)
                    {
                        userChoice++;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("  ");
                        arrowY++;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("->");
                    }
                    else
                    {
                        userChoice = 0;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("  ");
                        arrowY -= 5;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("->");
                    }
                }
                else if (key.Key == ConsoleKey.UpArrow)
                {
                    if (userChoice >= 1)
                    {
                        userChoice--;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("  ");
                        arrowY--;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("->");
                    }
                    else
                    {
                        userChoice = 5;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("  ");
                        arrowY += 5;
                        Console.SetCursorPosition(arrowX, arrowY);
                        Console.Write("->");
                    }
                }
                else if (key.Key == ConsoleKey.Enter)
                {
                    break;
                }
            }

            switch (userChoice)
            {
                case 0:
                    MinesweeperGame.ChosenLevel = Level.Beginner;
                    MinesweeperGame.playField = CreatePlayField(Level.Beginner);
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 1:
                    MinesweeperGame.ChosenLevel = Level.Intermediate;
                    MinesweeperGame.playField = CreatePlayField(Level.Intermediate);
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 2:
                    MinesweeperGame.ChosenLevel = Level.Expert;
                    MinesweeperGame.playField = CreatePlayField(Level.Expert);
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 3:
                    //Import file with controls and instructions
                    PrintInstructions();
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 4:
                    //show the highscore table
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    HighScores.PrintHighScore();
                    break;
                case 5:
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Environment.Exit(0);
                    break;
            }
        }

        private static void PrintInstructions()
        {
            Console.Clear();
            SetConsoleForMenu();
            Console.SetCursorPosition(0, 0);
            Console.ForegroundColor = ConsoleColor.Red;
            try
            {
                StreamReader readLogo = new StreamReader(@"..\..\TextFiles\Logo.txt");

                using (readLogo)
                {
                    string logo = readLogo.ReadToEnd();
                    Console.WriteLine(logo);
                }

                Console.ForegroundColor = ConsoleColor.Cyan;
                StreamReader readInstructions = new StreamReader(@"..\..\TextFiles\Instructions.txt");

                using (readInstructions)
                {
                    string instructions = readInstructions.ReadToEnd();
                    Console.WriteLine(instructions);
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File not found!");
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("The specified path is invalid.");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("The text file is empty.");
            }

            Console.ReadKey();
            Console.Clear();
            SelectMenu();
        }
    }
}
